package com.lzleang.jdomreader;

import com.lzleang.model.Student;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JOMReader {
    //xml解析相关
    public static void testJdomParser() throws JDOMException, IOException {
        File file = new File("src/student.xml");
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(file);
        Element root = document.getRootElement();
        List<Element> elements = root.getChildren();
        List<Student> students = new ArrayList<Student>();
        for(Element element:elements){
            Student student = new Student();
            //获取属性值
            List<Attribute> attributes = element.getAttributes();
            for(int i = 0 ; i < attributes.size();i++){
                String name = attributes.get(i).getName();
                if("id".equals(name)){
                    student.setId(attributes.get(i).getIntValue());
                }
            }

            //获取子节点
            List<Element> childrens = element.getChildren();
            for(Element children:childrens){
                if("name".equals(children.getName())){
                    student.setName(children.getText());
                }
                if("sex".equals(children.getName())){
                    student.setSex(children.getText());
                }
                if("age".equals(children.getName())){
                    student.setAge(Integer.parseInt(children.getText()));
                }
            }
            students.add(student);
        }
        System.out.println(students);
    }

    //属性相关操作
    public static void attributeHandler() throws JDOMException, IOException {
        File file = new File("src/student.xml");
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(file);
        Element rootElement = document.getRootElement();
        List<Element> student = rootElement.getChildren("student");
        Element element = student.get(0);
        //修改当前的id
        element.setAttribute("id","99");

        //创建一个新属性并赋值
        element.setAttribute("class","三年二班");

        //删除属性
        student.get(1).removeAttribute("id");
        //将doc写入文件
        saveXML(document);

    }

    //标签相关操作
    public static void updateDomHandler() throws JDOMException, IOException {
        File file = new File("src/student.xml");
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(file);
        Element rootElement = document.getRootElement();

        //创建一个父标签
        Element student = new Element("student");
        student.setAttribute("id","3");

        //创建子节点
        Element name = new Element("name");
        name.setText("周七");
        Element sex = new Element("sex");
        sex.setText("女");
        Element age = new Element("age");
        age.setText("28");

        //节点挂载
        student.addContent(name);
        student.addContent(sex);
        student.addContent(age);

        rootElement.addContent(student);


        //获取标签并修改内容
        Element element = rootElement.getChildren().get(0).getChild("name");
        element.setText("王五");

        //删除标签
        Element element1 = rootElement.getChildren().get(1);
        element1.getParent().removeContent(element1);

        //将doc写入文件
        saveXML(document);
    }


    //将doc写入文件
    public static void saveXML(Document doc) {
         // 将doc对象输出到文件
        try {
            // 创建xml文件输出流
             XMLOutputter xmlopt = new XMLOutputter();

            // 创建文件输出流
             FileWriter writer = new FileWriter("src/student.xml");

             // 指定文档格式
             Format fm = Format.getPrettyFormat();
             fm.setEncoding("UTF-8");
             xmlopt.setFormat(fm);

             // 将doc写入到指定的文件中
             xmlopt.output(doc, writer);
             writer.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

    public static void main(String[] args) throws JDOMException, IOException {
        updateDomHandler();
    }
}
